Registered S3 method overwritten by 'tsibble':
method from
as_tibble.grouped_df dplyr
Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':
interval
The following objects are masked from 'package:base':
intersect, setdiff, union
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
library(feasts) #autoplot visualizations
Loading required package: fabletools
Attaching package: 'fabletools'
The following object is masked from 'package:yardstick':
accuracy
The following object is masked from 'package:parsnip':
null_model
The following objects are masked from 'package:infer':
generate, hypothesize
library(fable)#If you prefer to keep the date as a date object, you can use lubridate::year() and lubridate::month() to extract the year and month, and then combine them:poudre_flow <-readNWISdv(siteNumber ="06752260", # Download data from USGS for site 06752260parameterCd ="00060", # Parameter code 00060 = discharge in cfsstartDate ="2013-01-01", # Set the start dateendDate ="2023-12-31") |># Set the end daterenameNWISColumns() |># Rename columns to standard names (e.g., "Flow", "Date")mutate(Date =as.Date(Date, format ="%Y-%m")) |># Create a Year-Month column as "YYYY-MM"group_by(Date) |># Group by the new Year-Month columnsummarise(Flow =mean(Flow, na.rm =TRUE)) # Calculate the average daily flow for each month
Convert to tsibble: Use as_tsibble() to convert the data.frame into a tsibble object. This will allow you to use the feast functions for time series analysis.
# Assuming you already have 'poudre_flow' with the "Date_YearMonth" column as shown earlierpoudre_flow_tsibble <- poudre_flow %>%as_tsibble() # Convert to tsibble using Date as the time index
Using `Date` as index variable.
Plotting the time series: Use ggplot to plot the time series data. Animate this plot with plotly
p_plot<- poudre_flow_tsibble %>%autoplot() +geom_line(color="purple") +labs(title="Interactive time series plot of the Poudre Rive flow",x="Date",y="Flow")
Plot variable not specified, automatically selected `.vars = Flow`
ggplotly(p_plot)
3. Subseries: Use gg_subseries to visualize the seasonal patterns in the data. This will help you identify any trends or seasonal cycles in the streamflow data. Describe what you see in the plot. How are “seasons” defined in this plot? What do you think the “subseries” represent?
#make month column to predict seasons + group by thempoudre_flow_tsibble <- poudre_flow_tsibble %>%mutate(month =month(Date)) %>%group_by(month)gg_subseries(poudre_flow_tsibble, Flow) +labs(title="Seasonal Patterns in Poudre River Stream Flow",y="Stream Flow",x="month",color= month) +theme(text =element_text(size =10), # Reduce font sizeaxis.text.x =element_text(angle =45, hjust =1), # Rotate x-axis text if neededstrip.text =element_text(size =9) # Resize month labels (facets) ) +theme_minimal()
The series shows a default of 12 months in the cycle, however it’s very disorganized. If it was beginning from January (1) than the trend shows that in the winter the flow is lower, then begins to pick up in spring months, before falling in summer and autumn months. Some years have better flow in their months. The subseries represents the months within the data for each year.
Decompose: Use the model(STL(…)) pattern to decompose the time series data into its components: trend, seasonality, and residuals. Chose a window that you feel is most appropriate to this data. Describe what you see in the plot. How do the components change over time? What do you think the trend and seasonal components represent?
# STL decomposition model doesn't assume constant seasonal effect like decompose()# Convert to a regular time series object for STL (monthly data)flow_ts <-ts(poudre_flow_tsibble$Flow, start =c(2013, 1), frequency =12)# Apply STL decompositionstl_decomposed <-stl(flow_ts, s.window ="periodic") %>%plot()
This plot shows the seasonal trends from 2013 and predicts them up until about 2350. They follow typical oscillations that occur on a yearly basis, except they slowly start having lower maximum flows. The trend could represent the effects of climate change on the river flows over time, and the seasonal component is driven by the typical natural biological processes as well as climate change (some seasons may start to get slightly shorter).